package hdoj;

import java.util.Scanner;

public class XORSum {

	private static class TrieNode{
		public TrieNode[] child;
		//只有0, 1
		public TrieNode() {
			//这里假定只包含0, 1
			child = new TrieNode[2];
		}
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int testCase = scanner.nextInt();
		int n, m;
		int[] array = new int[100000];
		for(int i = 1; i <= testCase; i++){
			n = scanner.nextInt();
			m = scanner.nextInt();
			for(int j = 0; j < n; j++){
				array[j] = scanner.nextInt();
			}
			TrieNode root = new TrieNode();
			for(int j = 0; j < n; j++){
				add(array[j], root);
			}
			System.out.println("Case #" + i + ":");
			for(int j = 0; j < m; j++){
				int cur = scanner.nextInt();
				System.out.println(search(cur, root));
			}
		}
		scanner.close();
	}
	
	
	
	public static void add(int x, TrieNode root){
		for(int i = 31; i >= 0; i--){  
	        int bit = (x >> i) & 1;  
	        if(root.child[bit] == null){
	        	root.child[bit] = new TrieNode();  
	        }
	        root = root.child[bit];
	    }
	}
	
	//查找x与其他所有值得最大异或值
	public static int search(int x, TrieNode root){
		int ans = 0;
		for(int i = 31; i >= 0; i--){  
	        int bit = (x >> i) & 1;  
	        // System.out.println("i位： " + i + " bit: " + bit + " root " + (root == null));
	        if(root.child[bit ^ 1] != null){
	        	root = root.child[bit ^ 1];
	        	ans = (ans << 1) + (bit ^ 1);
	        	// System.out.println("i位： " + i + " ans: " + ans);
	        }else{
	        	root = root.child[bit];
	        	ans = (ans << 1) + (bit);
	        }
	    }
		return ans;
	}
}
